home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 83 / MacAddict_083_2003-07.iso / mac / Software / Development / VLC Source 0.5.3.dmg / src / vlc.c < prev   
C/C++ Source or Header  |  2003-04-07  |  5KB  |  159 lines

  1. /*****************************************************************************
  2.  * vlc.c: the vlc player
  3.  *****************************************************************************
  4.  * Copyright (C) 1998-2001 VideoLAN
  5.  * $Id: vlc.c,v 1.19 2002/12/25 23:39:01 sam Exp $
  6.  *
  7.  * Authors: Vincent Seguin <seguin@via.ecp.fr>
  8.  *          Samuel Hocevar <sam@zoy.org>
  9.  *          Gildas Bazin <gbazin@netcourrier.com>
  10.  *          Lots of other people, see the libvlc AUTHORS file
  11.  *
  12.  * This program is free software; you can redistribute it and/or modify
  13.  * it under the terms of the GNU General Public License as published by
  14.  * the Free Software Foundation; either version 2 of the License, or
  15.  * (at your option) any later version.
  16.  *
  17.  * This program is distributed in the hope that it will be useful,
  18.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.  * GNU General Public License for more details.
  21.  *
  22.  * You should have received a copy of the GNU General Public License
  23.  * along with this program; if not, write to the Free Software
  24.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  25.  *****************************************************************************/
  26.  
  27. #include "config.h"
  28.  
  29. #include <stdio.h>                                              /* fprintf() */
  30. #include <stdlib.h>                                  /* putenv(), strtol(),  */
  31. #ifdef HAVE_SIGNAL_H
  32. #   include <signal.h>                            /* SIGHUP, SIGINT, SIGKILL */
  33. #endif
  34. #ifdef HAVE_TIME_H
  35. #   include <time.h>                                               /* time() */
  36. #endif
  37.  
  38. #include <vlc/vlc.h>
  39.  
  40. /*****************************************************************************
  41.  * Local prototypes.
  42.  *****************************************************************************/
  43. #ifndef WIN32
  44. static void SigHandler  ( int i_signal );
  45. #endif
  46.  
  47. /*****************************************************************************
  48.  * main: parse command line, start interface and spawn threads
  49.  *****************************************************************************/
  50. int main( int i_argc, char *ppsz_argv[] )
  51. {
  52.     int i_ret;
  53.  
  54.     fprintf( stderr, "VideoLAN Client %s\n", VLC_Version() );
  55.  
  56. #ifdef HAVE_PUTENV
  57. #   ifdef DEBUG
  58.     /* Activate malloc checking routines to detect heap corruptions. */
  59.     putenv( "MALLOC_CHECK_=2" );
  60.  
  61.     /* Disable the ugly Gnome crash dialog so that we properly segfault */
  62.     putenv( "GNOME_DISABLE_CRASH_DIALOG=1" );
  63. #   endif
  64.  
  65.     /* If the user isn't using VLC_VERBOSE, set it to 0 by default */
  66.     if( getenv( "VLC_VERBOSE" ) == NULL )
  67.     {
  68.         putenv( "VLC_VERBOSE=0" );
  69.     }
  70. #endif
  71.  
  72.     /* Create a libvlc structure */
  73.     i_ret = VLC_Create();
  74.     if( i_ret < 0 )
  75.     {
  76.         return i_ret;
  77.     }
  78.  
  79. #ifndef WIN32
  80.     /* Set the signal handlers. SIGTERM is not intercepted, because we need at
  81.      * least one method to kill the program when all other methods failed, and
  82.      * when we don't want to use SIGKILL.
  83.      * Note that we set the signals after the vlc_create call. */
  84.     signal( SIGINT,  SigHandler );
  85.     signal( SIGHUP,  SigHandler );
  86.     signal( SIGQUIT, SigHandler );
  87.  
  88.     /* Other signals */
  89.     signal( SIGALRM, SIG_IGN );
  90.     signal( SIGPIPE, SIG_IGN );
  91. #endif
  92.  
  93.     /* Initialize libvlc */
  94.     i_ret = VLC_Init( 0, i_argc, ppsz_argv );
  95.     if( i_ret < 0 )
  96.     {
  97.         VLC_Destroy( 0 );
  98.         return i_ret;
  99.     }
  100.  
  101.     /* Run libvlc, in non-blocking mode */
  102.     i_ret = VLC_Play( 0 );
  103.  
  104.     /* Add a blocking interface and keep the return value */
  105.     i_ret = VLC_AddIntf( 0, NULL, VLC_TRUE );
  106.  
  107.     /* Finish the threads */
  108.     VLC_Stop( 0 );
  109.  
  110.     /* Destroy the libvlc structure */
  111.     VLC_Destroy( 0 );
  112.  
  113.     return i_ret;
  114. }
  115.  
  116. #ifndef WIN32
  117. /*****************************************************************************
  118.  * SigHandler: system signal handler
  119.  *****************************************************************************
  120.  * This function is called when a fatal signal is received by the program.
  121.  * It tries to end the program in a clean way.
  122.  *****************************************************************************/
  123. static void SigHandler( int i_signal )
  124. {
  125.     static time_t abort_time = 0;
  126.     static volatile vlc_bool_t b_die = VLC_FALSE;
  127.  
  128.     /* Once a signal has been trapped, the termination sequence will be
  129.      * armed and subsequent signals will be ignored to avoid sending signals
  130.      * to a libvlc structure having been destroyed */
  131.  
  132.     if( !b_die )
  133.     {
  134.         b_die = VLC_TRUE;
  135.         abort_time = time( NULL );
  136.  
  137.         fprintf( stderr, "signal %d received, terminating vlc - do it "
  138.                          "again in case it gets stuck\n", i_signal );
  139.  
  140.         /* Acknowledge the signal received */
  141.         VLC_Die( 0 );
  142.     }
  143.     else if( time( NULL ) > abort_time + 2 )
  144.     {
  145.         /* If user asks again 1 or 2 seconds later, die badly */
  146.         signal( SIGINT,  SIG_DFL );
  147.         signal( SIGHUP,  SIG_DFL );
  148.         signal( SIGQUIT, SIG_DFL );
  149.         signal( SIGALRM, SIG_DFL );
  150.         signal( SIGPIPE, SIG_DFL );
  151.  
  152.         fprintf( stderr, "user insisted too much, dying badly\n" );
  153.  
  154.         abort();
  155.     }
  156. }
  157. #endif
  158.  
  159.